﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;

namespace WindowsFormsApplication1.RebornLogin
{
    public class LoginStatusRequest
    {
        public ClientSocketData clientSocketData;
        public bool usedResetPassword = false;
        private List<LoginStatusResult> results = new List<LoginStatusResult>();

        private Timer requestTimeout = new Timer(10000);

        public LoginStatusRequest(ClientSocketData csd)
        {
            clientSocketData = csd;
            requestTimeout.AutoReset = false;
            requestTimeout.Elapsed += new ElapsedEventHandler(requestTimeout_Elapsed);
        }

        public string result
        {
            get
            {
                lock (results)
                {
                    foreach (LoginStatusResult result in results)
                    {
                        if (result.clientExists != "notfound")
                            return result.clientExists;
                    }
                }
                return "notfound";
            }
        }

        private void requestTimeout_Elapsed(object sender, ElapsedEventArgs e)
        {
            Managers.loginManager.requestComplete(this);
        }

        public void sendRequests(List<ServerSocketData> servers)
        {
            if (servers.Count == 0)
            {
                Managers.loginManager.requestComplete(this);
                return;
            }

            //Build list of results we expect to recieve
            foreach (ServerSocketData server in servers)
            {
                LoginStatusResult result = new LoginStatusResult();
                result.server = server;
                results.Add(result);
            }
            string xmlMessage = "<clientexists userid=\"" + clientSocketData.UserID.ToString() + "\" clientid=\"" + clientSocketData.clientID.ToString() + "\" />";

            //Send request out to all servers, if send fails, remove from results list
            foreach (ServerSocketData ssd in servers)
            {
                if (SocketConnectionsManager.sendToServer(ssd, xmlMessage) == false)
                {
                    lock (results)
                    {
                        foreach (LoginStatusResult result in results)
                        {
                            if (result.server == ssd)
                            {
                                results.Remove(result);
                                break;
                            }
                        }
                        //If the send took a while to fail, we may have already gotten all responses, so we need to check
                        bool allComplete = true;
                        foreach (LoginStatusResult result in results)
                        {
                            if (result.hasResponded == false)
                                allComplete = false;
                        }
                        if (allComplete == true)
                        {
                            Managers.loginManager.requestComplete(this);
                            return;
                        }
                    }
                }
            }
            requestTimeout.Start();
        }
        public void processResult(ServerSocketData ssd, string clientExistsResult)
        {
            bool allResultsRecieved = true;
            lock (results)
            {
                foreach (LoginStatusResult result in results)
                {
                    if (result.server == ssd)
                    {
                        result.hasResponded = true;
                        result.clientExists = clientExistsResult;
                        if (clientExistsResult != "notfound")
                        {
                            allResultsRecieved = true;
                            break;
                        }
                    }
                    if (result.hasResponded == false)
                        allResultsRecieved = false;
                }
            }
            if (allResultsRecieved == true)
            {
                try
                {
                    requestTimeout.Stop();
                    requestTimeout.Dispose();
                }
                catch { }

                Managers.loginManager.requestComplete(this);
            }
        }
    }
}
